package com.zzg.mybatis.generator.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import com.zzg.mybatis.generator.model.GeneratorConfig;
import com.zzg.mybatis.generator.view.AlertUtil;

public class MyFileUtils {
	 /**
     * 检查并创建不存在的文件夹
     *
     * @return
     */
	public static boolean deleteAndCheckDirs(GeneratorConfig config) {
		List<String> dirs = new ArrayList<>();
		dirs.add(config.getProjectFolder());
		dirs.add(FilenameUtils.normalize(config.getProjectFolder().concat("/").concat(config.getModelPackageTargetFolder())));
		dirs.add(FilenameUtils.normalize(config.getProjectFolder().concat("/").concat(config.getDaoTargetFolder())));
		dirs.add(FilenameUtils.normalize(config.getProjectFolder().concat("/").concat(config.getMappingXMLTargetFolder())));
		boolean haveNotExistFolder = false;
		for (String dir : dirs) {
			File file = new File(dir);
			try {
				FileUtils.deleteDirectory(file);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			for (String dir : dirs) {
				FileUtils.forceMkdir(new File(dir));
			}
			return true;
		} catch (Exception e) {
			AlertUtil.showErrorAlert("创建目录失败，请检查目录是否是文件而非目录");
		}
        return true;
    }
}
